package com.dormitory.manage.controller;

import com.dormitory.manage.pojo.College;
import com.dormitory.manage.result.Result;
import com.dormitory.manage.service.CollegeService;
import com.dormitory.manage.service.ProfessionService;
import com.dormitory.manage.vo.CollegeDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description CollegeController
 * @date 2023/4/7 0007 23:51
 */
@RestController
@RequestMapping("college")
@CrossOrigin
@Slf4j
public class CollegeController {
    @Resource
    private CollegeService collegeService;

    @Resource
    private ProfessionService professionService;
    /**
    * @description 获取学院名称列表
    *
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  23:56
    */
    @GetMapping("getCollegeNameList")
    public Result getList(){
        List<String> collegeNameList = this.collegeService.getCollegeNameList();
        return Result.ok(collegeNameList);
    }

    @GetMapping("getCollegeListDetail")
    public Result getCollegeDetail(){
        List<CollegeDetailVo> collegeDetailList = this.collegeService.getCollegeListDetail();
        return Result.ok(collegeDetailList);
    }

    @DeleteMapping("removeCollege/{id}")
    public Result removeCollege(@PathVariable Long id){
        boolean result1 = this.collegeService.removeCollege(id);
        boolean result2 = this.professionService.removeProfessionByCollegeId(id);
        if (result1 && result2){
            return Result.ok("删除成功！");
        }
        return Result.fail();
    }

    @GetMapping("getCollegeInfo/{id}")
    public Result getCollegeInfo(@PathVariable Long id){
        College college = this.collegeService.getCollegeInfo(id);
        return Result.ok(college);
    }

    @PostMapping("updateCollege")
    public Result updateCollege(@RequestBody College college){
        boolean result = this.collegeService.updateCollege(college);
        if (result){
            return Result.ok();
        }
        return Result.fail();
    }

    @PostMapping("addCollege")
    public Result addCollege(@RequestBody College college){
        boolean result = this.collegeService.addCollege(college);
        if (result) {
            return Result.ok();
        }
        return Result.fail();
    }
}
